﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NetfractionHub.Network.Protocol;
using NetfractionHub.Network.Protocol.BASE.Factory;
using Netfraction.Common.Logging;

namespace NetfractionHub.LocalMachine.BotCommands.Handler
{
	// +hide, +hide --me, +hide alpha, +hide -user:"alpha"
	[CommandHandler("hide")]
	public class Hide : CommandHandler
	{
		public override BotReturnStatus HandleCommandMessage(NetfractionHub.RemoteMachine.Node source, CommandMessage message)
		{
			if (source == null)
				return new BotReturnStatus(false);

			string userToHide = string.Empty;

			if (message.ArgumentsCount == 0 || (message.BooleanArguments.Count == 1 && message.BooleanArguments.ContainsKey("me")))
			{
				if (!Security.Permission.HasPermission(source.SecurityIdentity, typeof(Security.Permissions.HideSelfPermission)))
					return new BotReturnStatus(false, Security.Permission.GetDeniedString(typeof(Security.Permissions.HideSelfPermission)));

				userToHide = source.Identity.Nickname;
			}
			else if (message.StringArguments.Count == 1)
			{
				if (!Security.Permission.HasPermission(source.SecurityIdentity, typeof(Security.Permissions.HideIdentityPermission)))
					return new BotReturnStatus(false, Security.Permission.GetDeniedString(typeof(Security.Permissions.HideIdentityPermission)));

				userToHide = message.StringArguments.ContainsKey("user") ? message.StringArguments["user"] : string.Empty;
			}
			else if (message.UnnamedArguments.Count == 1)
			{
				if (!Security.Permission.HasPermission(source.SecurityIdentity, typeof(Security.Permissions.HideIdentityPermission)))
					return new BotReturnStatus(false, Security.Permission.GetDeniedString(typeof(Security.Permissions.HideIdentityPermission)));

				userToHide = message.UnnamedArguments[0];
			}
			else
				return new BotReturnStatus(false, "Invalid parameters.");  // should reply with the help string for the function

			if (string.IsNullOrEmpty(userToHide))
				return new BotReturnStatus(false, "Invalid parameters.");

			RemoteMachine.Node nodeToHide = Network.ConnectionManager.GetConnectedNodeByNickname(userToHide);
			if (nodeToHide != null)
			{
				if (userToHide != source.Identity.Nickname && !Security.RoleManager.IsInAncestorTree(source.SecurityIdentity.Role, nodeToHide.SecurityIdentity.Role))
					return new BotReturnStatus(false, string.Format("Permission denied. You are not an ancestor of user '{0}'.", userToHide));

				string messageToRespond = messageToRespond = string.Format("Hiding '{0}'.", userToHide);

				this.ParentBot.SendEchoMessageFromBot(source, messageToRespond);

				LogManager.WriteEntry(messageToRespond, LogEventId.HideIdentity, LogLevel.Detailed);

				// TODO: don't do anything if the user is already hidden.
				nodeToHide.Identity.NodeType = nodeToHide.Identity.NodeType | IdentityNodeTypes.Hidden;
				Network.ConnectionManager.SendNodeInformationUpdateToAll(new Dictionary<int, object> { 
					{ (int)NetfractionHub.Network.Protocol.BASE.Factory.FactoryINF.ParameterEnum.SessionID, nodeToHide.SessionId },
					{ (int)NetfractionHub.Network.Protocol.BASE.Factory.FactoryINF.ParameterEnum.ClientType, (int)nodeToHide.Identity.NodeType } });
			}
			else
				return new BotReturnStatus(false, string.Format("Cannot hide, the user '{0}' does not exist.", userToHide));

			return new BotReturnStatus(false);
		}
	}
}
